//
//  KLMModeCustomTimeCell.swift
//  KLM_GN
//
//  Created by Nic on 2025/3/7.
//

import UIKit

typealias CustomTimeEndEditingBlock = (_ text: String?) -> ()

class KLMModeCustomTimeCell: UICollectionViewCell {

    @IBOutlet weak var tipLabel: UILabel!
    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var lineView: UIView!
    @IBOutlet weak var unitLabel: UILabel!
    
    var customTimeEndEditingBlock: CustomTimeEndEditingBlock?
    
    var modeCard: KLMCard! {
        didSet {
            textField.text = modeCard.title
            tipLabel.text = modeCard.tip
            unitLabel.text = modeCard.subTitle
        }
    }
    
    func startEditing() {
        textField.becomeFirstResponder()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        unitLabel.configStyle(font: UIFont(safeName: "PingFangSC-Medium", size: 12))
        tipLabel.configStyle(font: UIFont(safeName: "PingFangSC-Medium", size: 14), titleColor: rgba(129, 129, 129, 1))
        lineView.backgroundColor = rgba(198, 198, 198, 1)
        textField.keyboardType = .numberPad
        textField.font = UIFont(safeName: "PingFangSC-Heavy", size: 28)
        textField.borderStyle = .none
        
        layer.shadowColor = rgba(0, 0, 0, 0.17).cgColor
        layer.shadowOffset = CGSize(width: 0, height: 1)
        layer.shadowOpacity = 0.17
        layer.shadowRadius = 4
        layer.masksToBounds = false
    }

    override func layoutSubviews() {
        super.layoutSubviews()
//        backgroundView = UIImageView(image: UIImage.image(withColor: rgba(247, 247, 247, 1), size: bounds.size, cornerRadius: 8.0, borderColor: rgba(233, 233, 233, 1), borderWidth: 1))
        if modeCard.select {
            backgroundView = UIImageView(image: UIImage.image(withColor: rgba(246, 253, 250, 1), size: bounds.size, cornerRadius: 8.0, borderColor: appMainThemeColor, borderWidth: 0.5))
        } else {
            backgroundView = UIImageView(image: UIImage.image(withColor: rgba(247, 247, 247, 1), size: bounds.size, cornerRadius: 8.0))
        }
    }
    
    @IBAction func textFieldEndEditing(_ sender: UITextField) {
        if let endEditingBlock = customTimeEndEditingBlock {
            endEditingBlock(sender.text)
        }
    }
}
